package com.cuc.platform.core.componet.security.dao.jdbc;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.cuc.platform.core.componet.security.dao.SecurityResourceDao;
import com.cuc.platform.core.componet.security.domain.SecurityResource;

/**
 * @author peepul
 * 
 * 资源管理Jdbc实现类
 *
 */
@Repository("SecurityResourceDao")
public class SecurityRescDaoImpl  implements SecurityResourceDao {
		

	/**
	 * Constructor
	 */
	public SecurityRescDaoImpl() {
	}

	
	@Override
	public SecurityResource getRescById(Long id) {
		String sql = "select * from ss_resc where id = ?";

		return jdbcTemplate.queryForObject(sql, new Object[]{id}, new BeanPropertyRowMapper<SecurityResource>(SecurityResource.class) );
	}


	@Override
	public List<SecurityResource> getAllRescs() {
		StringBuffer sb = new StringBuffer();
		sb.append("select * from ss_resc order by priority asc");

		List<SecurityResource> list = jdbcTemplate.query(sb.toString(),
				new BeanPropertyRowMapper<SecurityResource>(SecurityResource.class));
		
		return list;
	}


	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.dao.SecurityResourceDao#insertResc(com.cuc.platform.core.componet.security.domain.SecurityResource)
	 */
	@Override
	public void insertResc(SecurityResource resc) throws DataAccessException {
		StringBuffer sb = new StringBuffer();
		sb.append("INSERT INTO ");
		sb.append(" ss_resc (");
		sb.append("name, res_type, res_string, priority, descn");
		sb.append(" ) VALUES ( ");
		sb.append(" ?, ?, ?, ?, ?  )");
		
		jdbcTemplate.update(sb.toString(), 
				new Object[]{resc.getName()      ,
							 resc.getRes_type()  ,
							 resc.getRes_string(),
							 resc.getPriority()  ,
							 resc.getDescn() 
				},
				
				new int[]{
					java.sql.Types.VARCHAR ,
					java.sql.Types.VARCHAR ,
					java.sql.Types.VARCHAR ,
					java.sql.Types.INTEGER ,
					java.sql.Types.VARCHAR
				} );
	}


	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.dao.SecurityResourceDao#updateResc(com.cuc.platform.core.componet.security.domain.SecurityResource)
	 */
	@Override
	public void updateResc(final SecurityResource resc) throws DataAccessException {
		String sql = "UPDATE ss_resc SET name = ?, res_type = ?, res_string = ?, priority = ?, descn = ?  WHERE id = ?";
		
		jdbcTemplate.update(sql, 
				new Object[]{
					 resc.getName()      ,
					 resc.getRes_type()  ,
					 resc.getRes_string(),
					 resc.getPriority()  ,
					 resc.getDescn()     ,
					 resc.getId()
				},
				
				new int[]{
					java.sql.Types.VARCHAR ,
					java.sql.Types.VARCHAR ,
					java.sql.Types.VARCHAR ,
					java.sql.Types.INTEGER ,
					java.sql.Types.VARCHAR ,
					java.sql.Types.NUMERIC
		} );

	}


	@Override
	public int delRescById(Long id) throws DataAccessException {
		String sql = "DELETE FROM ss_resc WHERE id = ? ";
		return jdbcTemplate.update(sql, id);
	}


	@Override
	public void delMultiResc(String... idArray)  throws DataAccessException {
		String sql = "DELETE FROM ss_resc WHERE id IN (?) ";
		jdbcTemplate.update(sql, idArray);
	}


	/* (non-Javadoc)
	 * @see com.cuc.platform.core.componet.security.dao.SecurityResourceDao#existPriority(java.lang.Integer)
	 *
	 * 查询表中是否存在录入的顺序值。
	 */
	@Override
	public Integer existPriority(Integer priority) {
		String sql = "SELECT count(*) FROM ss_resc WHERE priority = ?";
		
		Integer count = jdbcTemplate.queryForObject(sql, new Integer[]{priority}, Integer.class);
		return count;
	}

	
	/**
	 * Ioc
	 */
	@Resource
	private JdbcTemplate jdbcTemplate;

}
